<?php

namespace app\home\controller;


class Messgin extends Com
{
    public function index()
    {
        return view();
    }

    public function pass()
    {
        if (request()->isAjax()) {
            $User = db('mumber')->where('uid', session('user_id'))->find();
            if ($User['password'] != md5(input('post.oldpassword'))) {
                return ['row' => 0, 'fonts' => '旧密码不正确！'];
            }
            if ($User['password'] == md5(input('post.password'))) {
                return ['row' => 0, 'fonts' => '新密码与旧密码一致！'];
            }
            $User['password'] = md5(input('post.password'));
            $result = db('mumber')->update($User);
            if ($result) {
                return ['row' => 1, 'fonts' => '修改成功！'];
            } else {
                return ['row' => 0, 'fonts' => '修改失败！'];
            }
        }
        return view();
    }

    public function putpass()
    {
        if (request()->isAjax()) {
            $User = db('mumber')->where('uid', session('user_id'))->find();
            if ($User['putpassword'] != md5(input('post.oldpassword'))) {
                return ['row' => 0, 'fonts' => '旧密码不正确！'];
            }
            if ($User['putpassword'] == md5(input('post.password'))) {
                return ['row' => 0, 'fonts' => '新密码与旧密码一致！'];
            }
            $User['putpassword'] = md5(input('post.password'));
            $result = db('mumber')->update($User);
            if ($result) {
                return ['row' => 1, 'fonts' => '修改成功！'];
            } else {
                return ['row' => 0, 'fonts' => '修改失败！'];
            }
        }
        return view();
    }

    public function mess()
    {
        $user = db('mumber')->where('uid', session('user_id'))->find();
        if (request()->isAjax()) {
            $data = input('post.');
            if (!$data['nickname']) {
                return ['row' => 0, 'fonts' => '昵称不能为空！'];
            }
            $user['nickname'] = $data['nickname'];
            $user['sex'] = $data['sex'];
            $result = db('mumber')->update($user);
            if ($result) {
                return ['row' => 1, 'fonts' => '修改成功！'];
            } else {
                return ['row' => 0, 'fonts' => '修改失败！'];
            }
        }
        $this->assign('user', $user);
        return view();
    }

    public function put()
    {
        $user = db('mumber')->where('uid', session('user_id'))->find();
        if (request()->isAjax()) {
            $data = input('post.');
            if(db('mumber')->where('pay',$data['pay'])->count()>2){
                return ['row' => 0, 'fonts' => '支付宝绑定超出限制'];
            }
            if ($data['realname'] && $data['pay']) {
                $user['realname'] = $data['realname'];
                $user['pay'] = $data['pay'];
                $result = db('mumber')->update($user);
                if ($result) {
                    return ['row' => 1, 'fonts' => '修改成功！'];
                }
            }
            return ['row' => 0, 'fonts' => '修改失败！'];
        }
        $this->assign('user', $user);
        return view();
    }
}
